<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>getter 与 setter | JavaScript 高级</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.2.0">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
    

        
    
    
    <link rel="next" href="../e2bb52a93f2a04d3848c6bc059d4442d/index.html" />
    
    
    <link rel="prev" href="../e9c9c586570ff813bf127694b025d155/7c0fb98a684b27a818b1b546418cb62c.html" />
    

        
    </head>
    <body>
        
        
    <div class="book" data-level="3.8" data-basepath=".." data-revision="Sat Feb 27 2016 04:15:27 GMT+0800 (CST)">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
        
        
        

        

        
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        JavaScript 高级
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="449ee81808de646c3a7725ccd403a847/index.html">
            
                
                    <a href="../449ee81808de646c3a7725ccd403a847/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        基本概念复习
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="3ab0320f7a4ed5898fe45b7faccb54fd/index.html">
            
                
                    <a href="../3ab0320f7a4ed5898fe45b7faccb54fd/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        面向对象基础
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="3ab0320f7a4ed5898fe45b7faccb54fd/6314b9c802f768d42b4c3294598305c0.html">
            
                
                    <a href="../3ab0320f7a4ed5898fe45b7faccb54fd/6314b9c802f768d42b4c3294598305c0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        面向对象的基本概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="3ab0320f7a4ed5898fe45b7faccb54fd/39a0b92e978700a4f6f2a2bd2f2d8ba3.html">
            
                
                    <a href="../3ab0320f7a4ed5898fe45b7faccb54fd/39a0b92e978700a4f6f2a2bd2f2d8ba3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        JavaScript 中的内置对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="3ab0320f7a4ed5898fe45b7faccb54fd/4ed367f57d2b383ff22c531342b042b6.html">
            
                
                    <a href="../3ab0320f7a4ed5898fe45b7faccb54fd/4ed367f57d2b383ff22c531342b042b6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        Array 与 Array 常用方法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="3ab0320f7a4ed5898fe45b7faccb54fd/c16f2c36602b806d34e63671f14b0eda.html">
            
                
                    <a href="../3ab0320f7a4ed5898fe45b7faccb54fd/c16f2c36602b806d34e63671f14b0eda.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        Date 与 Date 常用方法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="3ab0320f7a4ed5898fe45b7faccb54fd/2c65b9b998e9058eda51931c6689a6ec.html">
            
                
                    <a href="../3ab0320f7a4ed5898fe45b7faccb54fd/2c65b9b998e9058eda51931c6689a6ec.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        包装对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="3ab0320f7a4ed5898fe45b7faccb54fd/da251d0d60d5f878f92d862aaedd8eb4.html">
            
                
                    <a href="../3ab0320f7a4ed5898fe45b7faccb54fd/da251d0d60d5f878f92d862aaedd8eb4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        自定义对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="3ab0320f7a4ed5898fe45b7faccb54fd/06ce5ce1bf4d31438cdcdc0275a9ea08.html">
            
                
                    <a href="../3ab0320f7a4ed5898fe45b7faccb54fd/06ce5ce1bf4d31438cdcdc0275a9ea08.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        对象字面量
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="3ab0320f7a4ed5898fe45b7faccb54fd/a502d4aea13dea8416d2e3c1849c0d2d.html">
            
                
                    <a href="../3ab0320f7a4ed5898fe45b7faccb54fd/a502d4aea13dea8416d2e3c1849c0d2d.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.8.</b>
                        
                        Error 对象与异常处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="3ab0320f7a4ed5898fe45b7faccb54fd/bac3b9472b2a4d7d14839e8c74e696ca.html">
            
                
                    <a href="../3ab0320f7a4ed5898fe45b7faccb54fd/bac3b9472b2a4d7d14839e8c74e696ca.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.9.</b>
                        
                        Math 对象与全局对象
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="e9c9c586570ff813bf127694b025d155/index.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        面向对象进阶
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="e9c9c586570ff813bf127694b025d155/c46418c7328d2a8b05cd6ccedd603f88.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/c46418c7328d2a8b05cd6ccedd603f88.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        面向对象基础复习
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="e9c9c586570ff813bf127694b025d155/40a6014bee4d6724e7aa800253818de5.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/40a6014bee4d6724e7aa800253818de5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        原型的基本概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="e9c9c586570ff813bf127694b025d155/f93b5f4ffff4f7cc386af737772af5d5.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/f93b5f4ffff4f7cc386af737772af5d5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        继承的概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="e9c9c586570ff813bf127694b025d155/9c3f525ec8bdda16f7618bcb3c936b98.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/9c3f525ec8bdda16f7618bcb3c936b98.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        Object 对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="e9c9c586570ff813bf127694b025d155/3c5d0a2365c2b15c5839ac6e1de9f9ea.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/3c5d0a2365c2b15c5839ac6e1de9f9ea.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        Function 对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="e9c9c586570ff813bf127694b025d155/3db73eb5d8866141920eb0d067ba68c3.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/3db73eb5d8866141920eb0d067ba68c3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        作用域链
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="e9c9c586570ff813bf127694b025d155/7c0fb98a684b27a818b1b546418cb62c.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/7c0fb98a684b27a818b1b546418cb62c.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                        闭包的基本概念
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="3.8" data-path="e9c9c586570ff813bf127694b025d155/bec81e72f2f0c7d6d17bd137036d3286.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/bec81e72f2f0c7d6d17bd137036d3286.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.8.</b>
                        
                        getter 与 setter
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="e2bb52a93f2a04d3848c6bc059d4442d/index.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        面向对象高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="e2bb52a93f2a04d3848c6bc059d4442d/5d3938919f20993f4b502f77cb59536f.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/5d3938919f20993f4b502f77cb59536f.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        原型链
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="e2bb52a93f2a04d3848c6bc059d4442d/b8c198488b5b2ca72efdae292161824f.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/b8c198488b5b2ca72efdae292161824f.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        函数的四中调用模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="e2bb52a93f2a04d3848c6bc059d4442d/0f37e1ef8250b257649a6d584bd33fb6.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/0f37e1ef8250b257649a6d584bd33fb6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        对象的创建模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="e2bb52a93f2a04d3848c6bc059d4442d/4fa341af02385d07831cfb4509d5bd20.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/4fa341af02385d07831cfb4509d5bd20.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        继承的方式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="e2bb52a93f2a04d3848c6bc059d4442d/a8d890e0bae896874f9af984daf86a53.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/a8d890e0bae896874f9af984daf86a53.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        闭包案例
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="e2bb52a93f2a04d3848c6bc059d4442d/6cc37fe6e9df40a64594e3417c638703.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/6cc37fe6e9df40a64594e3417c638703.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        正则表达式对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="e2bb52a93f2a04d3848c6bc059d4442d/0e3ef80c8612c6c6eb5baa8f397b9494.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/0e3ef80c8612c6c6eb5baa8f397b9494.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.7.</b>
                        
                        XMLHttpRequest 对象
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="6f394fa9c394d83e6cabd0ac644a8b2d/index.html">
            
                
                    <a href="../6f394fa9c394d83e6cabd0ac644a8b2d/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        封装框架基础
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="86469a999ebfad5a31cc516de283dbd9/index.html">
            
                
                    <a href="../86469a999ebfad5a31cc516de283dbd9/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        封装框架
                    </a>
            
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                Published with GitBook
            </a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Table of Contents"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Font Settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Share"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">
                    Share on Twitter
                </button>
                <button type="button" data-sharing="google-plus" class="button">
                    Share on Google
                </button>
                <button type="button" data-sharing="facebook" class="button">
                    Share on Facebook
                </button>
                <button type="button" data-sharing="weibo" class="button">
                    Share on Weibo
                </button>
                <button type="button" data-sharing="instapaper" class="button">
                    Share on Instapaper
                </button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Google"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Twitter"><i class="fa fa-twitter"></i></a>
    
    
    


    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >JavaScript 高级</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="getter-%E4%B8%8E-setter">getter &#x4E0E; setter</h1>
<p>&#x4E3B;&#x8981;&#x5185;&#x5BB9;:</p>
<ul>
<li>&#x5BF9;&#x8C61;&#x7279;&#x6027;</li>
<li>getter &#x4E0E; setter</li>
</ul>
<p>&#x5728; ES5 &#x4E2D;&#x52A0;&#x5165;&#x4E86;&#x5BF9;&#x8C61;&#x7684;&#x7279;&#x6027;, &#x672C;&#x8282;&#x5C31;&#x8BFB;&#x5199;&#x5668;&#x7279;&#x6027;&#x52A0;&#x4E00;&#x8BF4;&#x660E;.</p>
<h2 id="%E8%AF%BB%E5%86%99%E6%96%B9%E6%B3%95">&#x8BFB;&#x5199;&#x65B9;&#x6CD5;</h2>
<p>&#x6211;&#x4EEC;&#x77E5;&#x9053;, &#x5BF9;&#x8C61;&#x5C31;&#x662F;&#x952E;&#x503C;&#x5BF9; (&#x51FD;&#x6570;&#x5C31;&#x662F;&#x6570;&#x636E;). &#x4F46;&#x662F;&#x8BBF;&#x95EE;&#x5BF9;&#x8C61;&#x6570;&#x636E;&#x7684;&#x65F6;&#x5019;&#x6709;&#x65F6;&#x9700;&#x8981;&#x901A;&#x8FC7;&#x8BA1;&#x7B97;&#x5F97;&#x5230;. &#x4F8B;&#x5982;, &#x5229;&#x7528;&#x95ED;&#x5305;&#x6A21;&#x62DF;&#x79C1;&#x6709;&#x6570;&#x636E;&#x7684;&#x65F6;&#x5019;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> createPerson = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">name</span>) </span>{
    <span class="hljs-keyword">return</span> {
        get_Name: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
            <span class="hljs-keyword">return</span> name;
        },
        set_Name: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">value</span>) </span>{
            name = value;
        }
    };
};
<span class="hljs-keyword">var</span> p = createPerson(<span class="hljs-string">&quot;jk&quot;</span>);
<span class="hljs-built_in">console</span>.log(p.get_Name());
p.set_Name(<span class="hljs-string">&quot;&#x848B;&#x5764;&quot;</span>);
<span class="hljs-built_in">console</span>.log(p.get_Name());
</code></pre>
<p>&#x5728;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x4E2D;&#x662F;&#x65E9;&#x671F;&#x7684;&#x4E00;&#x4E9B;&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;, &#x53EF;&#x4EE5;&#x770B;&#x5230;&#x4E3B;&#x8981;&#x662F;&#x4E3A;&#x4E86;&#x8BBF;&#x95EE;&#x5BF9;&#x8C61;&#x7684; <code>name</code> &#x5E76;&#x5BF9;&#x5176;&#x505A;&#x4FEE;&#x6539;. &#x4F46;&#x662F;&#x5728;&#x4F7F;&#x7528;&#x7684;&#x65F6;&#x5019;&#x5E76;&#x4E0D;&#x662F;&#x76F4;&#x63A5;&#x5F53;&#x505A;&#x5C5E;&#x6027;&#x5728;&#x4F7F;&#x7528;, &#x800C;&#x662F;&#x4F5C;&#x4E3A;&#x65B9;&#x6CD5;&#x8FDB;&#x884C;&#x8C03;&#x7528;.</p>
<p>&#x7A0B;&#x5E8F;&#x5458;&#x5F88;&#x5F3A;&#x5927;, &#x5E0C;&#x671B;&#x5BF9;&#x4E8E;&#x8FD9;&#x4E2A;&#x64CD;&#x4F5C;&#x53EF;&#x4EE5;&#x50CF;&#x76F4;&#x63A5;&#x64CD;&#x4F5C;&#x53D8;&#x91CF;&#x4E00;&#x6837;, &#x8BBF;&#x95EE;&#x6570;&#x636E;, &#x4E0D;&#x518D;&#x4F7F;&#x7528; <code>p.get_Name()</code>, &#x800C;&#x662F;&#x76F4;&#x63A5;&#x4F7F;&#x7528; <code>p.name</code>; &#x4FEE;&#x6539;&#x6570;&#x636E;&#x4E0D;&#x518D;&#x4F7F;&#x7528; <code>p.set_Name(&quot;&#x848B;&#x5764;&quot;)</code>, &#x800C;&#x662F;&#x76F4;&#x63A5;&#x4F7F;&#x7528; <code>p.name = &quot;&#x848B;&#x5764;&quot;</code>. &#x6709;&#x670B;&#x53CB;&#x5C31;&#x8BF4;&#x4E86;. &#x90A3;&#x76F4;&#x63A5;&#x5B9A;&#x4E49;&#x6210;&#x5C5E;&#x6027;&#x4E0D;&#x5C31;&#x5B8C;&#x4E86;.</p>
<p>so, &#x95EE;&#x9898;&#x6765;&#x4E86;. &#x5982;&#x679C;&#x662F;&#x53EA;&#x5141;&#x8BB8;&#x8BFB;, &#x800C;&#x4E0D;&#x5141;&#x8BB8;&#x4FEE;&#x6539;&#x5462;? &#x518D;&#x6216;&#x8005;&#x6709;&#x4E00;&#x4E2A; <code>age</code> &#x5C5E;&#x6027;, &#x8981;&#x6C42;&#x4E0D;&#x5141;&#x8BB8;&#x8F93;&#x5165;&#x8D1F;&#x6570;&#x600E;&#x4E48;&#x529E;&#x5462;? ...</p>
<p>&#x8FD9;&#x6837;&#x7684;&#x95EE;&#x9898;&#x5F88;&#x591A;, &#x5F88;&#x660E;&#x663E;&#x5982;&#x679C;&#x6709;&#x4E00;&#x4E9B;&#x9650;&#x5236;, &#x90A3;&#x4E48;&#x5C31;&#x65E0;&#x6CD5;&#x4F7F;&#x7528;&#x5C5E;&#x6027;, &#x5FC5;&#x987B;&#x4F7F;&#x7528;&#x51FD;&#x6570;. &#x56E0;&#x6B64;&#x5728; JavaScript &#x4E2D;&#x5F15;&#x5165;&#x4E86;&#x4E00;&#x4E2A;&#x8BED;&#x6CD5;&#x7CD6;, getter &#x4E0E; setter &#x8BFB;&#x5199;&#x5668;.</p>
<h2 id="%E5%9F%BA%E6%9C%AC%E8%AF%AD%E6%B3%95">&#x57FA;&#x672C;&#x8BED;&#x6CD5;</h2>
<p>&#x9996;&#x5148;&#x8981;&#x5B9E;&#x73B0;&#x8BE5;&#x529F;&#x80FD;&#x7684;&#x57FA;&#x672C;&#x8BED;&#x6CD5;&#x4E3A;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> obj = {
    &#x53D8;&#x91CF;:&#x503C;,
    set &#x5C5E;&#x6027;&#x540D; (&#x53C2;&#x6570;) {
        setter &#x65B9;&#x6CD5;&#x4F53;
    },
    get &#x5C5E;&#x6027;&#x540D; () {
        getter &#x65B9;&#x6CD5;&#x4F53;
    }
};
</code></pre>
<p>&#x6CE8;&#x610F;, &#x8FD9;&#x91CC;&#x4F7F;&#x7528;&#x7684;&#x8BED;&#x6CD5;&#x4E0E;&#x4E4B;&#x524D;&#x7684;&#x8BED;&#x6CD5;&#x4E0D;&#x592A;&#x4E00;&#x6837;. &#x8FD9;&#x91CC;&#x4F7F;&#x7528; <code>get</code> &#x548C; <code>set</code> &#x5F15;&#x5BFC;. &#x6CA1;&#x6709;&#x4F7F;&#x7528; <code>function</code> &#x5173;&#x952E;&#x5B57;, &#x4F46;&#x662F;&#x521B;&#x5EFA;&#x7684;&#x4F9D;&#x65E7;&#x662F;&#x51FD;&#x6570; (&#x65B9;&#x6CD5;). &#x8FD9;&#x91CC;&#x6CA1;&#x6709;&#x5192;&#x53F7;, &#x76F4;&#x63A5;&#x5728;&#x540D;&#x5B57;&#x540E;&#x9762;&#x8DDF;&#x4E0A;&#x5706;&#x62EC;&#x53F7;&#x548C;&#x65B9;&#x6CD5;&#x4F53;. &#x8FD9;&#x91CC;&#x540D;&#x5B57;&#x53EF;&#x4EE5;&#x968F;&#x610F;&#x547D;&#x540D;, &#x4F46;&#x662F;&#x4E00;&#x822C;&#x4E0E;&#x9700;&#x8981;&#x7EA6;&#x675F;&#x7684;&#x53D8;&#x91CF;&#x540C;&#x540D;. <code>set</code> &#x65B9;&#x6CD5;&#x53C2;&#x6570;&#x540D;&#x662F;&#x968F;&#x610F;&#x7684;, &#x5728;&#x8D4B;&#x503C;&#x7684;&#x65F6;&#x5019;, &#x5C31;&#x4F1A;&#x8C03;&#x7528; setter &#x65B9;&#x6CD5;. &#x5E76;&#x5C06;&#x88AB;&#x8D4B;&#x503C;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x800C;&#x662F;&#x7528;. &#x5982;&#x679C;&#x5728;&#x8BFB;&#x53D6;&#x6570;&#x636E;&#x7684;&#x65F6;&#x5019;, &#x5219;&#x4F1A;&#x8C03;&#x7528; getter &#x65B9;&#x6CD5;, &#x4ECE;&#x800C;&#x8FD4;&#x56DE;&#x6570;&#x636E;.</p>
<p>&#x5C06;&#x524D;&#x9762;&#x7684;&#x6848;&#x4F8B;&#x8FDB;&#x884C;&#x6539;&#x5199;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> createPerson = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">name</span>) </span>{
    <span class="hljs-keyword">return</span> {
        get name () {
            <span class="hljs-keyword">return</span> name;
        },
        set name (value) {
            name = value;
        }
    };
};
<span class="hljs-keyword">var</span> p = createPerson(<span class="hljs-string">&quot;jk&quot;</span>);
<span class="hljs-built_in">console</span>.log(p.name);
p.name = <span class="hljs-string">&quot;&#x848B;&#x5764;&quot;</span>;
<span class="hljs-built_in">console</span>.log(p.name);
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x4E3A;:</p>
<p><img src="../images/Snip20160226_37.png" alt=""></p>
<p>&#x770B;&#x8D77;&#x6765;&#x4E66;&#x4E0D;&#x662F;&#x5F88;&#x7B80;&#x5355;. &#x8FD9;&#x6837;&#x5176;&#x5B9E;&#x548C;&#x8C03;&#x7528;&#x65B9;&#x6CD5;&#x662F;&#x4E00;&#x6837;&#x7684;, &#x53EA;&#x662F;&#x4F7F;&#x7528;&#x8D77;&#x6765;&#x66F4;&#x52A0;&#x8212;&#x670D;&#x4E0E;&#x65B9;&#x4FBF;.</p>
<p>&#x540C;&#x7406;, &#x6211;&#x4EEC;&#x5047;&#x8BBE;&#x7ED9;&#x5E74;&#x9F84;&#x8BBE;&#x7F6E;&#x6570;&#x503C;, &#x4F46;&#x662F;&#x4E0D;&#x5141;&#x8BB8;&#x8D4B;&#x503C;&#x4E3A;&#x8D1F;&#x6570;, &#x5982;&#x679C;&#x4E3A;&#x8D1F;&#x6570;&#x5C31;&#x629B;&#x51FA;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> createPerson = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">age</span>) </span>{
    <span class="hljs-keyword">return</span> {
        get age () {
            <span class="hljs-keyword">return</span> age;
        },
        set age (value) {
            <span class="hljs-keyword">if</span> (value &gt; <span class="hljs-number">0</span>) {
                age = value;
            } <span class="hljs-keyword">else</span> {
                <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">&quot;&#x5E74;&#x9F84;&#x4E0D;&#x5141;&#x8BB8;&#x4E3A;&#x8D1F;&#x6570;&quot;</span>);
            }
        }
    };
};
<span class="hljs-keyword">var</span> p = createPerson(<span class="hljs-number">0</span>);
<span class="hljs-built_in">console</span>.log(p.age);
p.age = <span class="hljs-number">123</span>;
<span class="hljs-built_in">console</span>.log(p.age);
<span class="hljs-keyword">try</span> {
    p.age = -<span class="hljs-number">1</span>;
    <span class="hljs-built_in">console</span>.log(p.age);
} <span class="hljs-keyword">catch</span> (e) {
    <span class="hljs-built_in">console</span>.log(e);
}
</code></pre>
<p>&#x6267;&#x884C;&#x7ED3;&#x679C;&#x4E3A;:</p>
<p><img src="../images/Snip20160226_38.png" alt=""></p>
<h2 id="%E5%B0%8F%E7%BB%93">&#x5C0F;&#x7ED3;</h2>
<ul>
<li>&#x5BF9;&#x8C61;&#x8BFB;&#x5199;&#x5C5E;&#x6027;&#x7684;&#x57FA;&#x672C;&#x8BED;&#x6CD5;</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../e9c9c586570ff813bf127694b025d155/7c0fb98a684b27a818b1b546418cb62c.html" class="navigation navigation-prev " aria-label="Previous page: 闭包的基本概念"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../e2bb52a93f2a04d3848c6bc059d4442d/index.html" class="navigation navigation-next " aria-label="Next page: 面向对象高级"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
